Skip to main content

Git

If you want to learn the details, a great resource is
https://git-scm.com/book/en/v2/Getting-Started-About-Version-Control

Command reference

Below is a summary of commands that should help working with Git.
Command references in blocks and examples in green

Configure

Get config parameters (--global flag indicates global config which applies to all repositories on the system, else local config specific to a repository)

git config --global --list git config --list

Set config prarameters

git config --global <config_name> <config_value> git config <config_name> <config_value>

     git config --global user.name "John Doe"
     git config --global user.email "johndoe@example.com"

note

Initial setup involves setting the git user name and email

Reset config

git config --global --unset <config_name> git config --unset <config_name>

Proxy

Setup git proxy if operating behind a proxy server

git config --global http.proxy http://<proxy_user>:<proxy_password>@<proxy_server>:<proxy_port>

     git config --global http.proxy http://ashanbogh:myStrongPass@proxy.techbraid.com:8080

Unset proxy

git config --global --unset http.proxy

Clone or Initialize

Clone an existing repository

git clone <url>

Examples
     git clone https://github.com/aws/aws-cli.git
     git clone git@github.com:aws/aws-cli.git

Initialize a new git repository

git init

This initialized a new git repository in the current working directory.

git init <directory>

This initialized a new git repository in the specified working directory.

Fetch

Fetch lets the local git repository scan changes that have occurred in the remote repository.

git fetch

Checkout

git checkout checks out the local branch.
If the local branch does not exist, and there exists a remote branch with the same name, then the remote branch is brought down to the local repository and the files checked out.
If the branch name does not exist in local repository or remote repository, the command throws an error.

git checkout <branch_name> git checkout <local_branch_name>

     git checkout awesomebranch

Create new branch

All files on the current branch are "copied" over to the new branch.
A new branch is always created in the local repository.
The local repository is on this newly created branch. (Head switched to the new branch)

git checkout -b <new_branch_name>

     git checkout -b newawesomebranch

Stage files to commit

git add <filename>

     git add index.js
      git add com/company/product/newprod/Logger.java
      git add .

Commit staged changes

git commit -m "<message>"

     git commit -m "My very meaningful commit message"

Push changes

Pushes new commits to the remote branch.

git push git push <origin> <branch_name>

If remote branch does not exist, you will have to provide the command as below:

git push --set-upstream <origin> <branch_name>

     git push --set-upstream origin temp

You can force push if you have performed a rebase or edited the local commit.
Under such circumstances, when you push, git rejects the push informing that the local and remote have diverged.
In order to overcome this on say a feature branch, you could use

git push --force.

danger

Force push ONLY If you're a 100% sure of what you're doing.
This overwrites the remote branch with local state of the branch.
NEVER do this on master!

Rebase

Rebases current branch with branch_to_rebase

git rebase <branch_to_rebase>

     git rebase master
      git rebase origin/master

Perform an interactive rebase on the current local branch from commit <commit_id> onwards.
The interactive rebase provides an opportunity to rename commit messages, change or drop commits.

git rebase -i <commit_id>

     git rebase -i 983a2dec29d70f6fe4325a90f22b1c1aa6fd6536

Merge

Merge a branch onto currently checked out branch.

git merge <branch_to_merge>

Merge local branch onto currently checked out branch
     git merge feature/story-123

Merge remote branch onto currently checked out branch
     git merge origin/feature/story-123

Delete local branch

git branch -D <branch_to_delete> // Delete a local branch

Delete remote branch

git push -d <remote> <remote_branch_to_delete> // Delete remote branch

     git push -d origin featyre/story-1422

List branches

List local branches

git branch --list git branch -v

List remote and local branches

git remote show <remote>

Git confit

git config --list

Git log

git log -<count>

     git log -3

Cherry pick

Apply a particular commit into current branch

git cherry-pick <commit_id>

Reset branch

Reset to remote discarding new local commits

git reset --hard

Reset to remote but retain changes of new local commits

git reset --soft